package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.GoodsDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;

import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.LinkedList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shopping")
public class ShoppingCartsController {

    @Autowired
    IProductService productService;
    @Autowired
    ICartService iCartService;
    @Autowired
    IHomeService iHomeService;

    @GetMapping("/product/{id}")
    public ResponseData queryProductDetail(@PathVariable Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        return new ResponseUtil().setData(productDetail);
    }

    @GetMapping("/goods")
    public ResponseData queryProductList(Integer page,Integer size,String sort,Integer priceGt,Integer priceLte,Long cid) {
        //cid是根据商品种类搜索 数据库item_cat表
        GoodsDto goodsDto = productService.queryProductList(page, size, sort, priceGt, priceLte, cid);
        return new ResponseUtil().setData(goodsDto);
    }

    @GetMapping("/recommend")
    public ResponseData queryProductRecommend() {
        //热门推荐板块
        Integer panelId = 6;

        LinkedList<PanelDto> panelDtos = new LinkedList<>();
        PanelDto panelDto = iHomeService.queryProductRecommend(panelId);
        panelDtos.add(panelDto);

        return new ResponseUtil().setData(panelDtos);
    }


    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCat(@PathVariable Long uid, @PathVariable Long pid) {
        iCartService.deleteCartItem(uid,pid);
        return new ResponseUtil().setData(null);
    }

    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckedCat(@PathVariable Long uid) {
        iCartService.deleteCheckedItem(uid);
        return new ResponseUtil().setData(null);
    }

    @PutMapping("items")
    public ResponseData checkAll(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        iCartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil().setData(null);
    }


    @GetMapping("/carts")
    public ResponseData getCartsList(HttpServletRequest request){
        //获取userId
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        String uid = object.get("uid").toString();

        List<CartProductDto> cartListById = iCartService.getCartListById(Long.valueOf(uid));

        return new ResponseUtil().setData(cartListById);
    }

    @PostMapping("/carts")
    public ResponseData addGoodsToCart(@RequestBody CartRequest cartRequest){

        iCartService.addToCart(cartRequest);
        return new ResponseUtil().setData(null);
    }

    @PutMapping("/carts")
    public ResponseData updateCartNum(@RequestBody CartRequest cartRequest){
        iCartService.updateCartNum(cartRequest);
        return new ResponseUtil().setData(null);
    }
}
